﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Devart.Data.PostgreSql;
using System.Data;

namespace Sistema_financiero.Entidades
{
    public class Pago
    {
        public int Id_prestamo { get; set; }
        public DateTime Fecha { get; set; }
        public TimeSpan Hora { get; set; }
        public decimal Valor { get; set; }
        public Int64 nro_pago { get; set; }

        public static List<Pago> Obtener_pagos(int prestamo_seleccionado)
        {
            List<Pago> lista = new List<Pago>();
            using (PgSqlConnection conexion = new PgSqlConnection(frm_Menu.conexion))
            {
                conexion.Unicode = true;
                conexion.Open();
                string sql = String.Format("SELECT * FROM obtener_pagos({0})", prestamo_seleccionado);
                PgSqlCommand comando = new PgSqlCommand(sql, conexion);
                PgSqlDataReader reader = comando.ExecuteReader();
                while (reader.Read())
                {
                    lista.Add(Cargar_Pago(reader));
                }
                conexion.Close();
            }
            return lista;

        }


        private static Pago Cargar_Pago(IDataReader reader)
        {
            string hora;
            DateTime hora_;
            Pago item = new Pago();

            item.Id_prestamo = Convert.ToInt32(reader[0]);
            item.Fecha = Convert.ToDateTime(reader[1]);

            hora = Convert.ToString(reader[2]);
            if (hora.Length > 8)
            {
                hora = hora.Remove(8);
            }
            hora_ = Convert.ToDateTime(hora);
            item.Hora = hora_.TimeOfDay;

            item.Valor = Convert.ToDecimal(reader[3]);
            item.nro_pago = Convert.ToInt64(reader[4]);


            return item;
        }

        public static List<Pago> Obtener_pagos(int prestamo_seleccionado, DateTime fecha_actual)
        {
            List<Pago> lista = new List<Pago>();
            using (PgSqlConnection conexion = new PgSqlConnection(frm_Menu.conexion))
            {
                conexion.Unicode = true;
                conexion.Open();
                string sql = String.Format("SELECT * FROM obtener_pagos_hoy({0},'{1}')", prestamo_seleccionado, (fecha_actual.ToString("d")));
                PgSqlCommand comando = new PgSqlCommand(sql, conexion);
                PgSqlDataReader reader = comando.ExecuteReader();
                while (reader.Read())
                {
                    lista.Add(Cargar_Pago(reader));
                }
                conexion.Close();
            }
            return lista;
        }

        public static void Guardar_pago(int id_prestamo, decimal valor)
        {
            using (PgSqlConnection conexion = new PgSqlConnection(frm_Menu.conexion))
            {
                conexion.Unicode = true;
                conexion.Open();

                string sql = String.Format("INSERT INTO \"Pago\" (\"id_prestamo\", \"valor\") VALUES ({0}, '{1}')", id_prestamo, valor);
                PgSqlCommand comando = new PgSqlCommand(sql, conexion);
                PgSqlDataReader reader = comando.ExecuteReader();
                reader.Read();

                conexion.Close();


            }
        }

        public static void Anular_pago(int id_prestamo, Int64 nro_pago)
        {
            using (PgSqlConnection conexion = new PgSqlConnection(frm_Menu.conexion))
            {
                conexion.Unicode = true;
                conexion.Open();

                string sql = String.Format("DELETE FROM \"Pago\" WHERE \"Pago\".id_prestamo = {0} AND \"Pago\".nro_pago ={1}", id_prestamo, nro_pago);
                PgSqlCommand comando = new PgSqlCommand(sql, conexion);
                PgSqlDataReader reader = comando.ExecuteReader();
                reader.Read();

                conexion.Close();


            }
        }
    }
}
